package com.ruoyi.production.service.impl.graftinglabel;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.graftinglabel.GraftinglabelPurchaseMapper;
import com.ruoyi.production.domain.graftinglabel.GraftinglabelPurchase;
import com.ruoyi.production.service.graftinglabel.IGraftinglabelPurchaseService;
import com.ruoyi.production.domain.graftinglabel.GraftinglabelAmount;
import com.ruoyi.production.mapper.graftinglabel.GraftinglabelAmountMapper;

/**
 * 嫁接签购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class GraftinglabelPurchaseServiceImpl implements IGraftinglabelPurchaseService
{
    @Autowired
    private GraftinglabelPurchaseMapper graftinglabelPurchaseMapper;
    @Autowired
    private GraftinglabelAmountMapper graftinglabelAmountMapper;

    /**
     * 查询嫁接签购买信息
     *
     * @param graftinglabelPurchaseId 嫁接签购买信息ID
     * @return 嫁接签购买信息
     */
    @Override
    public GraftinglabelPurchase selectGraftinglabelPurchaseById(Long graftinglabelPurchaseId)
    {
        return graftinglabelPurchaseMapper.selectGraftinglabelPurchaseById(graftinglabelPurchaseId);
    }

    /**
     * 查询嫁接签购买信息列表
     *
     * @param graftinglabelPurchase 嫁接签购买信息
     * @return 嫁接签购买信息
     */
    @Override
    public List<GraftinglabelPurchase> selectGraftinglabelPurchaseList(GraftinglabelPurchase graftinglabelPurchase)
    {
        return graftinglabelPurchaseMapper.selectGraftinglabelPurchaseList(graftinglabelPurchase);
    }

    /**
     * 新增嫁接签购买信息
     *
     * @param graftinglabelPurchase 嫁接签购买信息
     * @return 结果
     */
    @Override
    public int insertGraftinglabelPurchase(GraftinglabelPurchase graftinglabelPurchase)
    {
        return graftinglabelPurchaseMapper.insertGraftinglabelPurchase(graftinglabelPurchase);
    }

    /**
     * 修改嫁接签购买信息
     *
     * @param graftinglabelPurchase 嫁接签购买信息
     * @return 结果
     */
    @Override
    public int updateGraftinglabelPurchase(GraftinglabelPurchase graftinglabelPurchase)
    {
        Long graftinglabelPurchaseId = graftinglabelPurchase.getGraftinglabelPurchaseId();
        //数据库存储的数据
        GraftinglabelPurchase PurchaseDB = selectGraftinglabelPurchaseById(graftinglabelPurchaseId);
        //数据库存储的原数据的总量
        GraftinglabelAmount originAmounts = new GraftinglabelAmount();
        originAmounts.setGraftinglabelSpecification(PurchaseDB.getGraftinglabelSpecification());
        originAmounts.setGraftinglabelTypeId(PurchaseDB.getGraftinglabelTypeId());
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        List<GraftinglabelAmount> amounts = graftinglabelAmountMapper.selectGraftinglabelAmountList(originAmounts);
        GraftinglabelAmount originAmount = amounts.get(0);
        if (PurchaseDB.getGraftinglabelSpecification().equals(graftinglabelPurchase.getGraftinglabelSpecification())&&PurchaseDB.getGraftinglabelTypeId().equals(graftinglabelPurchase.getGraftinglabelTypeId())&&PurchaseDB.getOriginCompany().equals(graftinglabelPurchase.getOriginCompany())){
            originAmount.setStorageAmount(originAmount.getStorageAmount()+graftinglabelPurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount());
            graftinglabelAmountMapper.updateGraftinglabelAmount(originAmount);
        }else {
            GraftinglabelAmount newAmounts = new GraftinglabelAmount();
            newAmounts.setGraftinglabelTypeId(graftinglabelPurchase.getGraftinglabelTypeId());
            newAmounts.setGraftinglabelSpecification(graftinglabelPurchase.getGraftinglabelSpecification());
            newAmounts.setOriginCompany(graftinglabelPurchase.getOriginCompany());
            List<GraftinglabelAmount> amounts1 = graftinglabelAmountMapper.selectGraftinglabelAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)){
                newAmounts.setStorageAmount(graftinglabelPurchase.getPurchaseAmount());
                graftinglabelAmountMapper.insertGraftinglabelAmount(newAmounts);
            }else {
                GraftinglabelAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount()+graftinglabelPurchase.getPurchaseAmount());
                graftinglabelAmountMapper.updateGraftinglabelAmount(newAmount);
            }
            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            graftinglabelAmountMapper.updateGraftinglabelAmount(originAmount);
        }
        return graftinglabelPurchaseMapper.updateGraftinglabelPurchase(graftinglabelPurchase);
    }

    /**
     * 批量删除嫁接签购买信息
     *
     * @param graftinglabelPurchaseIds 需要删除的嫁接签购买信息ID
     * @return 结果
     */
    @Override
    public int deleteGraftinglabelPurchaseByIds(Long[] graftinglabelPurchaseIds)
    {
        for (Long graftinglabelPurchaseId : graftinglabelPurchaseIds){
            GraftinglabelPurchase graftinglabelPurchase=selectGraftinglabelPurchaseById(graftinglabelPurchaseId);
            GraftinglabelAmount graftinglabelAmount=new GraftinglabelAmount();
            graftinglabelAmount.setGraftinglabelSpecification(graftinglabelPurchase.getGraftinglabelSpecification());
            graftinglabelAmount.setGraftinglabelTypeId(graftinglabelPurchase.getGraftinglabelTypeId());
            graftinglabelAmount.setOriginCompany(graftinglabelPurchase.getOriginCompany());
            List<GraftinglabelAmount> amounts = graftinglabelAmountMapper.selectGraftinglabelAmountList(graftinglabelAmount);
            GraftinglabelAmount graftinglabelAmount1 = amounts.get(0);
            graftinglabelAmount1.setStorageAmount(graftinglabelAmount1.getStorageAmount()-graftinglabelPurchase.getPurchaseAmount());
            graftinglabelAmountMapper.updateGraftinglabelAmount(graftinglabelAmount1);
        }
        return graftinglabelPurchaseMapper.deleteGraftinglabelPurchaseByIds(graftinglabelPurchaseIds);
    }

    /**
     * 删除嫁接签购买信息信息
     *
     * @param graftinglabelPurchaseId 嫁接签购买信息ID
     * @return 结果
     */
    @Override
    public int deleteGraftinglabelPurchaseById(Long graftinglabelPurchaseId)
    {
        GraftinglabelPurchase graftinglabelPurchase=selectGraftinglabelPurchaseById(graftinglabelPurchaseId);
        GraftinglabelAmount graftinglabelAmount=new GraftinglabelAmount();
        graftinglabelAmount.setGraftinglabelSpecification(graftinglabelPurchase.getGraftinglabelSpecification());
        graftinglabelAmount.setGraftinglabelTypeId(graftinglabelPurchase.getGraftinglabelTypeId());
        graftinglabelAmount.setOriginCompany(graftinglabelPurchase.getOriginCompany());
        List<GraftinglabelAmount> amounts = graftinglabelAmountMapper.selectGraftinglabelAmountList(graftinglabelAmount);
        GraftinglabelAmount graftinglabelAmount1 = amounts.get(0);
        graftinglabelAmount1.setStorageAmount(graftinglabelAmount1.getStorageAmount()-graftinglabelPurchase.getPurchaseAmount());
        graftinglabelAmountMapper.updateGraftinglabelAmount(graftinglabelAmount1);
        return graftinglabelPurchaseMapper.deleteGraftinglabelPurchaseById(graftinglabelPurchaseId);
    }

    /**
     * 插入嫁接签购买表更新嫁接签量表
     * @param graftinglabelPurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(GraftinglabelPurchase graftinglabelPurchase){
        GraftinglabelAmount graftinglabelAmount = new GraftinglabelAmount();
        graftinglabelAmount.setGraftinglabelSpecification(graftinglabelPurchase.getGraftinglabelSpecification());
        graftinglabelAmount.setGraftinglabelTypeId(graftinglabelPurchase.getGraftinglabelTypeId());
        graftinglabelAmount.setOriginCompany(graftinglabelPurchase.getOriginCompany());
        List<GraftinglabelAmount> graftinglabelAmounts = graftinglabelAmountMapper.selectGraftinglabelAmountList(graftinglabelAmount);
        graftinglabelAmount.setStorageAmount(graftinglabelPurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(graftinglabelAmounts)) {
            GraftinglabelAmount amount = graftinglabelAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + graftinglabelPurchase.getPurchaseAmount());
            graftinglabelAmountMapper.updateGraftinglabelAmount(amount);
        } else {
            graftinglabelAmountMapper.insertGraftinglabelAmount(graftinglabelAmount);
        }
        return insertGraftinglabelPurchase(graftinglabelPurchase);

    }
}
